﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_HouseData : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    HouseDao hd = new HouseDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            if (uv.Ugroup == "管理员")
            {
                dt = hd.getHouse("", "0", "99999", "0","0");
                gvhouse.DataSource = dt;
                gvhouse.DataBind();
                for (int i = 0; i < gvhouse.Rows.Count; i++)
                {
                    string id = dt.Rows[i][0].ToString();
                    id = em.Encript(id);
                    HyperLink hlname = (HyperLink)gvhouse.Rows[i].Cells[0].FindControl("hlname");
                    hlname.NavigateUrl = "HouseInfo.aspx?hid=" + id;
                    HyperLink hlupdate = (HyperLink)gvhouse.Rows[i].Cells[0].FindControl("hlupdate");
                    hlupdate.NavigateUrl = "HouseUpdate.aspx?hid=" + id;
                    Label labtype = (Label)gvhouse.Rows[i].Cells[0].FindControl("labtype");
                    switch (labtype.Text)
                    {
                        case "1": labtype.Text = "商品房"; break;
                        case "2": labtype.Text = "经济适用房"; break;
                        case "3": labtype.Text = "商业租房"; break;
                    }
                }
            }
            else
            {
                Response.Redirect("../index.aspx");
            }
        }
    }
    protected void gvhouse_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvhouse.PageIndex = e.NewPageIndex;
        dt = hd.getHouse("", "0", "99999", "0","0");
        gvhouse.DataSource = dt;
        gvhouse.DataBind();
        for (int i = 0; i < gvhouse.Rows.Count; i++)
        {
            int size = e.NewPageIndex * 20 + i;
            string id = dt.Rows[size][0].ToString();
            id = em.Encript(id);
            HyperLink hlname = (HyperLink)gvhouse.Rows[i].Cells[0].FindControl("hlname");
            hlname.NavigateUrl = "HouseInfo.aspx?hid=" + id;
            HyperLink hlupdate = (HyperLink)gvhouse.Rows[i].Cells[0].FindControl("hlupdate");
            hlupdate.NavigateUrl = "HouseUpdate.aspx?hid=" + id;
        }
    }
    protected void gvhouse_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "del")
        {
            hd.delHouse(id);
        }
        dt = hd.getHouse("", "0", "99999", "0", "0");
        gvhouse.DataSource = dt;
        gvhouse.DataBind();
        for (int i = 0; i < gvhouse.Rows.Count; i++)
        {
            string hid = dt.Rows[i][0].ToString();
            hid = em.Encript(hid);
            HyperLink hlname = (HyperLink)gvhouse.Rows[i].Cells[0].FindControl("hlname");
            hlname.NavigateUrl = "HouseInfo.aspx?hid=" + hid;
            HyperLink hlupdate = (HyperLink)gvhouse.Rows[i].Cells[0].FindControl("hlupdate");
            hlupdate.NavigateUrl = "HouseUpdate.aspx?hid=" + hid;
        }
    }
    protected void gvhouse_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[10].Attributes.Add("onclick", "return confirm('确认删除吗？')");
        }
    }
}
